//
//  NewsTableViewController.m
//  
//
//  Created by admin on 17/3/2.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "LCHomeChannelListsTableViewController.h"
//#import "NSDictionary+Data.h"
#import "LCHomeChannelListsTableViewController+LoadData.h"

//#define HomeChannelListsTableViewControllerCell @"HomeChannelListsTableViewControllerCell"
#define LCEssayCell @"LCEssayCell"
#define LCEssayImagesCell @"LCEssayImagesCell"
#define LCEssayVideoCell @"LCEssayVideoCell"

@interface LCHomeChannelListsTableViewController ()

@end

@implementation LCHomeChannelListsTableViewController

//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [self.tableView reloadData];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.beautifulEssayListArray = [NSMutableArray array];
    [self setupTableView];
    [self setupMJRefresh];//添加下拉刷新
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyTabBarDoubleClick:) name:NOTIFY_TABBAR_DOUBLE_CLICK object:nil];
}
-(void)notifyTabBarDoubleClick:(NSNotification *)noti{
    NSDate *date = [[NSDate alloc] init];
    if (date.timeIntervalSince1970 - _lastDate.timeIntervalSince1970 > 1) {
        if ([_cid isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"didSelectChannelAtCid"]]) {
            [self.tableView.mj_header beginRefreshing];
            self.numberPage = 1;
            [self loadDataArray:_cid :self.numberPage];
        }
        _lastDate = date;
    }
}
- (void)setupTableView {
    self.tableView.backgroundColor = RGBColor(236, 236, 236);
    [self.tableView registerNib:[UINib nibWithNibName:@"LCEssayTableViewCell" bundle:nil] forCellReuseIdentifier:LCEssayCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"LCHomeEssayImagesCell" bundle:nil] forCellReuseIdentifier:LCEssayImagesCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"LCHomeEssayVideoCell" bundle:nil] forCellReuseIdentifier:LCEssayVideoCell];
    self.tableView.tableFooterView = [[UIView alloc] init];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.tableView lc_setupUITableViewCellXian];
}
- (void)setupMJRefresh {
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.edgesForExtendedLayout = UIRectEdgeNone;//就可以正常
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        weakSelf.numberPage = 1;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadDataArray:_cid :weakSelf.numberPage];
            if (weakSelf.tableView.mj_header.isRefreshing) {
                [weakSelf.tableView.mj_header endRefreshing];
            }
        });
    }];
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        weakSelf.numberPage++;
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadDataArray:_cid :weakSelf.numberPage];
            [weakSelf.tableView.mj_header endRefreshing];
        });
    }];
}
- (void)setCid:(NSString *)cid{
    _cid = cid;
    if (self.numberPage == 0) {
        self.numberPage = 1;
        [self loadDataArray:_cid :self.numberPage];
    }
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.beautifulEssayListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier;
    BeautifulEssayModel *model = self.beautifulEssayListArray[indexPath.row];
    
    //多张图片
    if (model.cover.count > 2) {
        LCHomeEssayImagesTableViewCell *cell;
        cellIdentifier = LCEssayImagesCell;
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[LCHomeEssayImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.model = self.beautifulEssayListArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{
        //文章
        if ([model.type isEqualToString:@"3"]) {
            LCHomeEssayRightImageTableViewCell *cell;
            cellIdentifier = LCEssayCell;
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell) {
                cell = [[LCHomeEssayRightImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
            cell.model = self.beautifulEssayListArray[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        } else {
            //视频样式
            LCHomeEssayVideoTableViewCell *cell;
            cellIdentifier = LCEssayVideoCell;
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell) {
                cell = [[LCHomeEssayVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
            cell.model = self.beautifulEssayListArray[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    
}
#pragma mark -  首页其他标签页跳转  -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BeautifulEssayModel *model = self.beautifulEssayListArray[indexPath.row];
    NSLog(@"model.remoteUrl = %@", model.remoteUrl);
    [LSZSpecialPackaging increaseTheNumberOfViews:model.id :^(BOOL isOK) {

    }];
    if ([model.type isEqualToString:@"1"] || [model.type isEqualToString:@"2"]) {//1. 音频   2.视频
        LSZVideoWebView *viewController = [[LSZVideoWebView alloc] init];
        viewController.link = model.shareUrl;
        viewController.model = model;
        [self.navigationController pushViewController:viewController animated:YES];
        return;
    }
    LCWenHTMLViewController *viewController = [[LCWenHTMLViewController alloc] init];
    viewController.link = self.beautifulEssayListArray[indexPath.row].remoteUrl;
    viewController.model = self.beautifulEssayListArray[indexPath.row];
    [self.navigationController pushViewController:viewController animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    BeautifulEssayModel *model = self.beautifulEssayListArray[indexPath.row];
    
    if (model.cover.count > 2) {
        if (ScreenWidth == 320) {
            return 161;
        }
        else if (ScreenWidth == 375) {
            return 174.5;
        }
        else if (ScreenWidth == 414) {
            return 184;
        }
    }

    if ([model.type isEqualToString:@"3"]) {
        if (ScreenWidth == 320) {
            return 86.5+4;
        }
        else if (ScreenWidth == 375) {
            return 98+4;
        }
        else if (ScreenWidth == 414) {
            return 106+4;
        }
    }

    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:[UIScreen mainScreen].bounds.size.width];
}

@end
